package com.own.core.object.admin.pages;

import java.lang.reflect.Field;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.own.core.html.form.validateType;
import com.own.core.html.form.dropDown.DropDown;
import com.own.core.html.form.object.ObjectField;
import com.own.core.html.form.textArea.TextArea;
import com.own.core.html.form.textField.TextField;
import com.own.core.object.Dao;
import com.own.core.object.admin.Action;
import com.own.core.object.admin.Role;
import com.own.core.operation.Controller;

@Entity
@Table(name = "core_page")
@NamedQueries({
		@NamedQuery(name = "page.loadAllMainPage", query = " from Page page where page.parentPage is  null "),
		@NamedQuery(name = "page.loadMainPage", query = "select  distinct p.parentPage   from Page p  "
				+ " join p.roles  r "
				+ " join r.groups g "
				+ " join g.users  u " + " where u.id=:userId"),
		@NamedQuery(name = "page.loadAllSubPage", query = "from Page page where page.parentPage is not  null "),
		@NamedQuery(name = "page.loadSubPage", query = "select  distinct p   from Page p  "
				+ " join p.roles  r "
				+ " join r.groups g "
				+ " join g.users  u "
				+ " where u.id=:userId "
				+ " and  p.parentPage.pageName =:mainPageName "),
		@NamedQuery(name = "page.loadBySystemName", query = "from Page page where page.className =:className ") })
public class Page extends Dao {

	private long id;

	@TextField(required = true, list = true)
	private String pageName;

	@TextField(required = true, list = true)
	private String messageKey; //rename to by resource key

	@TextField(required = false, list = false, validate = validateType.CLASS)
	private String className;

	@TextField(required = false, list = false, validate = validateType.CLASS)
	private String controllerClassName;

	@DropDown(NamedQuery = "page.loadAllMainPage")
	Page parentPage;

	@TextArea(required = false, list = false)
	private String loadQuery;

	@TextField(validate = validateType.NUMERIC)
	private long orderNo;

	@TextField(validate = validateType.ALPHA_NUMERIC, list = false)
	private String iconName;
	
	
	@ObjectField
	private Set<Action> action;//supported action throw this page

//	
//	@ObjectField
	private Set<Role> roles;
	
	//need 
	
//action list avaible action

	
	private Set<Component> component;

	public Page(String name) {
		this.pageName = name;

	}

	public Page(String name, String className, Page parentMenu) {
		this.pageName = name;
		this.parentPage = parentMenu;
		this.className = className;
	}

	public Page(Class classObj) {
		className = classObj.getName();
	}

	public Class createClass() {
		try {
			return Class.forName(className);
		} catch (Exception e) {
			return null;
		}
	}

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	@Transient
	public String getName() {
		return pageName;
	}

	public Page() {
	}

	// bi-directional many-to-one association to Role
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "parent_id")
	public Page getParentPage() {
		return parentPage;
	}

	public void setParentPage(Page parentPage) {
		this.parentPage = parentPage;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	@OneToMany(mappedBy = "page")
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	@Override
	public void delete() {
		// String sql = "delete  from role where objectRole_id=" + getId();
		// DbOperation.ME.executeQuery(sql);

	}

	public String getLoadQuery() {
		return loadQuery;
	}

	public void setLoadQuery(String loadQuery) {
		this.loadQuery = loadQuery;
	}

	public String getControllerClassName() {
		return controllerClassName;
	}

	public void setControllerClassName(String controllerClassName) {
		this.controllerClassName = controllerClassName;
	}

	public Controller generateControllerObject() {
		try {
			return (Controller) Class.forName(controllerClassName)
					.newInstance();
		} catch (Exception e) {
			return null;
		}
	}

	public String getPageName() {
		return pageName;
	}

	public void setPageName(String menuName) {
		this.pageName = menuName;
	}

	public String getMessageKey() {
		return messageKey;
	}

	public void setMessageKey(String systemName) {
		this.messageKey = systemName;
	}

}
